<template>
	<view>
		<view class="img_list">
			<view class="img_item" v-for="(item,index) in picList" :key="index" @click="changSelect(item)">
				<image class="pic" :src="item.path" mode=""></image>
				<image class="select_icon" :src="item.isSelect? 'https://mg-print.oss-cn-shenzhen.aliyuncs.com/img/select.png' :'https://mg-print.oss-cn-shenzhen.aliyuncs.com/img/notselect.png'"
					mode=""></image>
			</view>
		</view>
		<view class="select_all">
			<view class="select_all_left">
				<view class="left">
					<image :src="imageSrc" mode="" @click="changeSelectAll">
					</image>
					<text>全选</text>
				</view>
				<text class="select_all_price">共选择{{selectedCount}}页</text>
			</view>
			<button :class="['print_button',selectedCount==0?'cantpay':'']" :disabled="selectedCount==0"
				@click="back">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				picList: [{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
					{
						path: 'https://img.3dmgame.com/uploads/images/news/20210820/1629425354_858267.jpg',
						isSelect: false
					},
				],
				isSelectedAll: false
			};
		},
		onLoad(options) {
			if (options.FileCode) {

			}
			if (options.page) {
				console.log(options.page);
				this.changePartSelect(options.page)
			}
		},
		methods: {
			 parseFormattedArray(formattedArr) {
			  let result = [];
			
			  formattedArr.forEach(item => {
			    if (item.includes('-')) {
			      // 如果是范围格式的字符串
			      const [start, end] = item.split('-').map(Number);
			      for (let i = start; i <= end; i++) {
			        result.push(i);
			      }
			    } else {
			      // 单个数字的情况
			      result.push(Number(item));
			    }
			  });
			
			  return result;
			},
			changePartSelect(string){
				console.log(string);
				let arr = string.split(',')
				arr =this.parseFormattedArray(arr)
				console.log(arr);
				arr.forEach(item => this.picList[item-1].isSelect=true)
			},
			changSelect(item) {
				item.isSelect = !item.isSelect
			},
			changeSelectAll() {
				// 全选反选
				this.isSelectedAll = !this.isSelectedAll
				this.picList.forEach((item) => {
					item.isSelect = this.isSelectedAll
				})
			},
			back() {
				let arr = []
				this.picList.forEach((item, index) => {
					if (item.isSelect) {
						arr.push(index + 1)
					}
				})
				uni.setStorageSync('selectArr', arr)
				uni.navigateBack()
				// console.log(arr);
			}
		},
		computed: {
			isAllSelect() {
				return this.picList.every(item => item.isSelect === true);

			},
			selectedCount() {
				return this.picList.filter(file => file.isSelect).length;
			},
			imageSrc() {
				return this.isSelectedAll ? 'https://mg-print.oss-cn-shenzhen.aliyuncs.com/img/select.png' : 'https://mg-print.oss-cn-shenzhen.aliyuncs.com/img/notselect.png';
			},
		},
		watch: {
			isAllSelect(newValue) {
				this.isSelectedAll = newValue;
			}
		},
	}
</script>

<style lang="scss" scoped>
	.select_all {

		.select_all_price {
			// font-size: 40rpx;
			// font-weight: bold;
			margin-left: 30rpx;
			// color: red;
		}

		.select_all_left {
			display: flex;
			align-items: center;

			.left {
				display: flex;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
			}
		}

		.print_button {
			height: 100%;
			width: 200rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: white;
			background-color: #0d78fa;
			font-size: 28rpx;
		}

		.cantpay {
			background-color: #e6e6eb;
		}

		display: flex;
		align-items: center;
		position: fixed;
		bottom: 0;
		height: 100rpx;
		padding-left: 20rpx;
		width: 100%;
		justify-content: space-between;
		z-index: 99;
		background-color: #ffffff;
	}

	.img_list {
		display: flex;
		flex-wrap: wrap;
		padding: 0 20rpx;
		gap: 20rpx; // 添加间隙，替代 margin-right

		.img_item {
			flex: 1 1 calc(33.333% - 20rpx); // 每个图片项占 1/3 宽度减去间隙
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			// width: 210rpx;
			max-width: calc(33.333% - 20rpx);
			height: 210rpx;

			.pic {
				width: 100%; // 使图片填满图片项的宽度
				height: 100%; // 保持图片比例
			}

			.select_icon {
				position: absolute;
				right: 0;
				bottom: 0;
				width: 40rpx;
				height: 40rpx;
			}
		}
	}
</style>